﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Model;
using System.Windows.Shapes;
using System.Windows.Media;

namespace HowHigh.UI
{
    public partial class Map : UserControl
    {
        private List<Point> _locations = new List<Point>();
        private double _zoom;
        private Point _centerLocation;
        private static Brush _brush = new SolidColorBrush(Colors.Green);
        public Map()
        {
            InitializeComponent();
            G.Children.Clear();
        }
        
        public void AddLocation(Location location)
        {
           
            if (location != null)
            {
                var p = new Point(location.Longitude, location.Latitude);
                if(!_locations.Contains(p))
                    _locations.Add(p);
                Draw();
            }
        }
        private void Draw()
        {
            if (_locations.Count > 1)
            {
                var maxY = _locations.Max(i => i.Y);
                var minY = _locations.Min(i => i.Y);
                var maxX = _locations.Max(i => i.X);
                var minX = _locations.Min(i => i.X);
                var difX = maxX - minX;
                var difY = maxY - minY;

                var cX = (maxX + minX) / 2;
                var cY = (maxY + minY) / 2;

                var difMax = Math.Max(difX, difY) * 1.2;
                _zoom = this.ActualWidth / difMax;
                _centerLocation = new Point(cX, cY);
            }
            else
            {
                _zoom = 1;
                _centerLocation = _locations.First();
            }

            var centerMargin = new Point()
            {
                X = this.ActualWidth / 2 -10,
                Y = this.ActualHeight / 2 -10
            };

            var margins = _locations.Select(i => i.Subtract(_centerLocation).Times(_zoom).Add(centerMargin)).ToList();

            G.Children.Clear();

            margins.ForEach(i => 
            {
                G.Children.Add(NewIcon(i));
            });
        }

        private Ellipse NewIcon(Point margin)
        {
            //<Ellipse Height="20" HorizontalAlignment="Left" Margin="224,197,0,0" Name="ellipse1" StrokeThickness="1" VerticalAlignment="Top" Width="20" Fill="#FF3F893F" />

            var e = new Ellipse
            {
                Width = 20,
                Height = 20,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                VerticalAlignment = System.Windows.VerticalAlignment.Top,
                StrokeThickness = 1,
                Fill = _brush,
                Margin = new Thickness(margin.X,margin.Y,0,0)
            };
            return e;
        }
    }

    public static class PointHelper
    {
        public static Point Add(this Point self,Point point)
        {
            if (self == null || point == null)
                return new Point(0,0);
            return new Point(self.X + point.X, self.Y + point.Y);
        }

        public static Point Subtract(this Point self, Point point)
        {
            if (self == null || point == null)
                return new Point(0,0);
            return new Point(self.X - point.X, self.Y - point.Y);
        }

        public static Point Times(this Point self,double time)
        {
            if (self == null)
                return new Point(0, 0);
            return new Point(self.X *time, self.Y *time);
        }

    }
}
